define(['jquery', 'bootstrap', 'select2', 'validate', 'validate.en', 'koala-search', 'shiftform', 'modalSearch',
         ],
function($, validate) {
  return {
    
    init: function() {
      this.initPage();
      this.initWarehouse();
      this.initVehicle();
      this.initCompany();
      this.initClient();
      this.triggerModalSearch();
    },
    
    initPage: function() {
      
      $(document).find('form').bind('reset', function(e){
         $(this).find(".select2").each(function(e){
           if(!$(this).hasClass("multiple")) {
           $(this).select2("val", "");
           }
         })
      })
        
      $(document).find('form').bt_validate();
      
      $('select[name="organizationId"]').select2({
        theme: "classic",
        placeholder : '请选择',
        minimumResultsForSearch : -1,
        allowClear : true,
      });
      
      $('select[name="temporary"]').select2({
        theme: "classic",
        placeholder : '请选择',
        minimumResultsForSearch : -1,
        allowClear : true,
      });
      
    },
    
    initWarehouse: function() {
      
      $('[data-target="#modal-search-fromWarehouse"]').modalsearch({
        url: window.__CTX__.root + '/warehouses/query',
        tableColumns: ['起始地代码', '起始地名称'],
        tableValues: ['code', 'name'],
        displayValues: ['code', 'name'],
        values: ['id']
      });
      
      $('[data-target="#modal-search-toWarehouse"]').modalsearch({
        url: window.__CTX__.root + '/warehouses/query',
        tableColumns: ['目的地代码', '目的地名称'],
        tableValues: ['code', 'name'],
        displayValues: ['code', 'name'],
        values: ['id']
      });
      
      var options = {
        time: 1000,
        url: window.__CTX__.root + "/warehouses/query",
        queryName: "code",
        value: ["id"],
        display: ["code", "name"]
      }
      
      $('.fromWarehouse-search').koalaSearch(options);
      $('.toWarehouse-search').koalaSearch(options);
      
      $(document).on("koala.search.value.changed", ".fromWarehouse-search, .toWarehouse-search", function(){
        console.log($(this).next().val());
        // TODO 其他查询后的逻辑
      })
    },
    
    initVehicle:function() {
        
      $('[data-target="#modal-search-vehicle"]').modalsearch({
      url: function(){
        var url = window.__CTX__.root + '/vehicles/query';
        return url;
      },
      tableColumns: ['驳运车代码', '驳运车名称'],
      tableValues: ['code', 'name'],
      displayValues: ['code', 'name'],
      values: ['id']
      });
        
      var options = {
        time: 1000,
        url: window.__CTX__.root + "/vehicles/query",
        queryName: "code",
        value: ["id"],
        display: ["code", "name"]
      }
      
      $('.vehicle-search').koalaSearch(options);
    },
      
    initCompany:function() {
         
    	$('[data-target="#modal-search-company"]').modalsearch({
        url: window.__CTX__.root + '/transportation-companys/query',
        tableColumns: ['运输公司代码', '运输公司名称'],
        tableValues: ['code', 'name'],
        displayValues: ['code', 'name'],
        values: ['id']
      });
      
       
       var options = {
       time: 1000,
       url: window.__CTX__.root + "/transportation-companys/query",
       queryName: "code",
       value: ["id"],
       display: ["code", "name"]
       }
     
       $('.company-search').koalaSearch(options);
     },
       
     initClient:function() {
         
	    $('[data-target="#modal-search-client"]').modalsearch({
	    url: window.__CTX__.root + '/clients/query',
	    tableColumns: ['客户代码', '客户名称'],
	    tableValues: ['code', 'name'],
	    displayValues: ['code', 'name'],
	    values: ['id']
	    });
	    
	    var options = {
	    time: 1000,
	    url: window.__CTX__.root + "/clients/query",
	    queryName: "code",
	    value: ["id"],
	    display: ["code", "name"]
	    }
	    
	    $('.client-search').koalaSearch(options);
	  },
	  
	  triggerModalSearch: function() {
      $('.modal-search').each(function() {
        var value = $(this).val();
        console.info(value);
        if (value) {
          $(this).trigger('blur');
        }
      });
    }
	  
  }
})